package com.example.check_oa.model.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.check_oa.model.mapper.*;
import com.example.comment_oa.pojos.attendance.BusinessTripAp;
import com.example.comment_oa.pojos.attendance.HolidayAp;
import com.example.comment_oa.pojos.attendance.HolidayType;
import com.example.comment_oa.pojos.attendance.OvertimeAp;
import com.example.check_oa.model.service.HolidayTypeService;
import com.example.check_oa.utils.GetAntten;
import com.example.comment_oa.pojos.MouthAt;
import org.apache.commons.io.FilenameUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
* @author cjl
* @description 针对表【holiday_type】的数据库操作Service实现
* @createDate 2022-10-23 19:15:02
*/
@Service
public class HolidayTypeServiceImpl extends ServiceImpl<HolidayTypeMapper, HolidayType>
    implements HolidayTypeService{
    private String url="D:\\study\\gitee\\my_springcloud_project01cangku\\capacity\\src\\views\\capacity\\cjl\\img\\holi";
    @Resource
    HolidayTypeMapper holidayTypeMapper;
    @Resource
    HolidayApMapper holidayApMapper;
    @Resource
    StringRedisTemplate stringRedisTemplate;
    @Resource
    OvertimeApMapper overtimeApMapper;
    @Resource
    BusinessTripApMapper businessTripApMapper;
    @Resource
    MouthAtMapper mouthAtMapper;

    //查询假期余额
    @Override
    public List<HolidayType> holiNum(String userNo) {
        String user = stringRedisTemplate.opsForValue().get("user");
        JSONObject jsonObject = JSONObject.parseObject(user);
        JSONObject sysEmployee = JSONObject.parseObject(jsonObject.getString("sysEmployee"));
        String empSex = sysEmployee.getString("empSex");
        QueryWrapper<HolidayType> wrapper=new QueryWrapper<>();
        if(empSex.equals("1")){
            wrapper.ne("holiday_type_name","产假");
        }
        wrapper.ne("HOLIDAY_TYPE_ID",5);
        List<HolidayType> holidayTypes = holidayTypeMapper.selectList(wrapper);
        for (int i = holidayTypes.size() - 1; i >= 0; i--) {
            QueryWrapper<HolidayAp> wrapper1=new QueryWrapper<>();
            wrapper1.eq("HO_STATE",1);
            wrapper1.eq("USER_HO",userNo);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
            Date date = new Date();
            wrapper1.like("HO_AP_TIME",sdf.format(date));
            wrapper1.select("SUM(HO_TIME) as holiDaynum");
            wrapper1.eq("holiday_type_id",holidayTypes.get(i).getHolidayTypeId());
            HolidayAp holidayAp = holidayApMapper.selectOne(wrapper1);
            if(holidayAp!=null){
                holidayTypes.get(i).setLostholedayNum(holidayTypes.get(i).getHoledayNum()-holidayAp.getHoliDaynum());
            }else{
                holidayTypes.get(i).setLostholedayNum(holidayTypes.get(i).getHoledayNum());
            }
        }
        return holidayTypes;
    }

    //查询假期种类
    @Override
    public List<HolidayType> holiType(int sex) {
        QueryWrapper<HolidayType> wrapper=new QueryWrapper<>();
        if(sex==1){
            wrapper.ne("holiday_type_name","产假");
        }
        return holidayTypeMapper.selectList(wrapper);
    }

    //add
    @Override
    public int add(HolidayAp holidayAp, MultipartFile file) {
        holidayAp.setHoApStartTime(holidayAp.getTime().get(0));
        holidayAp.setHoApEndTime(holidayAp.getTime().get(1));
        //用户输入的请假日
        List<String> betweenDate = GetAntten.getBetweenDate(DateUtil.formatDate(holidayAp.getHoApStartTime()), DateUtil.formatDate(holidayAp.getHoApEndTime()));
        //今年的工作日
        List<String> yearWorkDay = GetAntten.getYearWorkDay();
        //上月已归档
        QueryWrapper<MouthAt> wrapper8=new QueryWrapper<>();
        Date hoApStartTime = holidayAp.getHoApStartTime();
        wrapper8.like("mouth",DateUtil.formatDate(hoApStartTime).substring(0,7));
        List<MouthAt> mouthAts = mouthAtMapper.selectList(wrapper8);
        if(!mouthAts.isEmpty()){
            return 6;
        }
        //用户输入and工作日两集合交集
        List<String> stringList = GetAntten.intersectList2(yearWorkDay, betweenDate);
        if(stringList.size()==0){
            //请假中没有工作日，时间无效，重新请假
            return 0;
        }
        //查询该用户全部请假日期
        QueryWrapper<HolidayAp> wrapper1=new QueryWrapper<>();
        wrapper1.eq("user_ho",holidayAp.getUserHo()).and(i->i.eq("ho_state",1).or().eq("ho_state",0));
        List<HolidayAp> holidayAps = holidayApMapper.selectList(wrapper1);
        List<String> betweenDate1=new ArrayList<>();
        holidayAps.forEach(e->{
            betweenDate1.addAll(GetAntten.getBetweenDate(DateUtil.formatDate(e.getHoApStartTime()), DateUtil.formatDate(e.getHoApEndTime())));
        });
        //已请假和输入交集，如有相同，返回该日期已有请假记录，或正在审批
        List<String> stringList1 = GetAntten.intersectList2(betweenDate1, betweenDate);
        if(stringList1.size()>0){
            //请假日期重复
            return 2;
        }
        //出差日期
        ArrayList<String> ii1 = new ArrayList<>();
        QueryWrapper<BusinessTripAp> wrapper=new QueryWrapper<>();
        wrapper.eq("user_bt",holidayAp.getUserHo()).and(i->i.eq("bt_ap_state",1).or().eq("bt_ap_state",0));
        List<BusinessTripAp> businessTripAps = businessTripApMapper.selectList(wrapper);
        businessTripAps.forEach(e->{
            ii1.addAll(GetAntten.getBetweenDate(DateUtil.formatDate(e.getBtStartTime()), DateUtil.formatDate(e.getBtEndTime())));
        });
        if(GetAntten.intersectList2(ii1,betweenDate).size()>0){
            //和出差日期重复
            return 5;
        }
        //加班集合
        List<String> ii=new ArrayList<>();
        QueryWrapper<OvertimeAp> ovWrapper=new QueryWrapper<>();
        ovWrapper.eq("user_ot",holidayAp.getUserHo()).and(i->i.eq("ot_state",1).or());
        ovWrapper.eq("ot_state",1).or().eq("ot_state",0);
        List<OvertimeAp> overtimeAps = overtimeApMapper.selectList(ovWrapper);
        overtimeAps.forEach(e-> {
            ii.addAll(GetAntten.getBetweenDate(DateUtil.formatDate(e.getOtStartTime()), DateUtil.formatDate(e.getOtEndTime())));
        });
        if(GetAntten.intersectList2(ii,betweenDate).size()>0){
            //请假日期存在加班时间
            return 4;
        }
        //不是事假都要进入，判断是否还有假期余额
        if(holidayAp.getHolidayTypeId()!=5){
            //已存在的某个类型请假的次数
            System.out.println(holidayAp.getHolidayTypeId());
            int integer = holidayApMapper.andHoliType(holidayAp.getUserHo(), holidayAp.getHolidayTypeId());
            System.out.println("已存在："+integer);
            integer=integer+stringList.size();
            System.out.println("已存在2："+integer);
            QueryWrapper<HolidayType> wrapper2=new QueryWrapper<>();
            wrapper2.eq("holiday_type_id",holidayAp.getHolidayTypeId());
            HolidayType holidayType = holidayTypeMapper.selectOne(wrapper2);
            if(integer>holidayType.getHoledayNum()){
                //已存在的某个类型请假的次数and输入的请假天数大于规定请假额度
                return 3;
            }
            holidayAp.setHoIsMoney(holidayType.getIsmoney());
        }
        if(file!=null){
            //获得文件的名称
            String fileName = file.getOriginalFilename();
            fileName = file.getOriginalFilename();
            //取后缀名
            String ext = FilenameUtils.getExtension(fileName);
            //创建一个新的文件名
            fileName = UUID.randomUUID().toString() +"."+ ext;
            File f =  new File(url,fileName);
            try {
                file.transferTo(f);//进行文件转移
            } catch (IOException e) {
                e.printStackTrace();
            }
            String filePath = "capacity/cjl/img/holi"+fileName;
            holidayAp.setHoPath(filePath);
        }
        holidayAp.setHoTime((double) stringList.size());
    return holidayApMapper.insert(holidayAp);
    }
}




